﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Smled.States
{
    public partial class RandomBrushToolbar : ToolStrip
    {
        public RandomBrushToolbar()
        {
            InitializeComponent();
            btnAdd.Click += new EventHandler(btnAdd_Click);
            cbBrushes.SelectedIndexChanged += new EventHandler(cbBrushes_SelectedIndexChanged);

            btnSave.Click += new EventHandler(btnSave_Click);
            btnLoad.Click += new EventHandler(btnLoad_Click);
        }

        void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Random brushes for Smled (*.rbrush)|*.rbrush";
            if (ofd.ShowDialog(MainForm.Instance) != DialogResult.Cancel)
            {
                if (File.Exists(ofd.FileName))
                {
                    RandomBrush rb = new RandomBrush();
                    rb.LoadFromFile(ofd.FileName, MainForm.Instance.MainView.Map.Set);
                    RefreshList();
                    cbBrushes.SelectedItem = rb;
                }
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            RandomBrush mb = SelectedBrush;
            if (mb != null)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Random brushes for Smled (*.rbrush)|*.rbrush";
                if (sfd.ShowDialog(MainForm.Instance) != DialogResult.Cancel)
                {
                    mb.SaveToFile(sfd.FileName);
                }
            }
        }

        void cbBrushes_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateButtons();
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            RandomBrush mb = new RandomBrush();
            bool res = RandomBrushWizard.ShowFor(mb);
            //System.Diagnostics.Debug.WriteLine("Dialog result : " + res);
            if (!res) RandomBrushStorage.Brushes.Remove(mb);
            RefreshList();
        }

        public void RefreshList()
        {
            cbBrushes.Items.Clear();
            foreach (RandomBrush brush in RandomBrushStorage.Brushes)
                cbBrushes.Items.Add(brush);

            UpdateButtons();
        }

        private void UpdateButtons()
        {
            if (SelectedBrush != null)
            {
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                btnSave.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
                btnSave.Enabled = false;
            }
        }

        public RandomBrush SelectedBrush
        {
            get
            {
                if (cbBrushes.SelectedItem is RandomBrush)
                {
                    return (RandomBrush)cbBrushes.SelectedItem;
                }
                return null;
            }
        }
    }
}
